package com.aabte.permissionframeworklearn.permission.controller;

import com.aabte.permissionframeworklearn.permission.common.ResponseBean;
import com.aabte.permissionframeworklearn.permission.model.SysRole;
import com.aabte.permissionframeworklearn.permission.model.SysUser;
import com.aabte.permissionframeworklearn.permission.param.ChangeUserParam;
import com.aabte.permissionframeworklearn.permission.param.RoleParam;
import com.aabte.permissionframeworklearn.permission.service.*;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Daniel
 */
@Slf4j
@Validated
@Controller
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysTreeService sysTreeService;

    @Autowired
    private SysRoleAclService sysRoleAclService;

    @Autowired
    private SysRoleUserService sysRoleUserService;

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/role.page")
    public ModelAndView rolePage() {
        return new ModelAndView("role");
    }

    @GetMapping("/list.json")
    @ResponseBody
    public ResponseBean getAll() {
        return ResponseBean.success(sysRoleService.getAll());
    }

    @PostMapping("/save.json")
    @ResponseBody
    public ResponseBean saveRole(
            @RequestBody RoleParam param
    ) {
        return ResponseBean.success(sysRoleService.save(param));
    }

    @PutMapping("/update.json")
    @ResponseBody
    public ResponseBean updateRole(
            @RequestBody RoleParam param
    ) {
        return ResponseBean.success(sysRoleService.update(param));
    }

    @GetMapping("/roleTree.json")
    @ResponseBody
    public ResponseBean roleTree(
            @RequestParam("roleId") Long roleId
    ) {
        return ResponseBean.success(sysTreeService.roleTree(roleId));
    }

    @PutMapping("/changeAcls.json")
    @ResponseBody
    public ResponseBean changeAcl(
            @RequestParam Long roleId,
            @RequestParam List<Long> aclIds
    ) {
        sysRoleAclService.changeAcl(roleId, aclIds);
        return ResponseBean.success();
    }

    @GetMapping("/users.json")
    @ResponseBody
    public ResponseBean users(
            @RequestParam("roleId") Long roleId
    ) {
        List<SysUser> selectedUserList = sysRoleUserService.getUsersByRoleId(roleId);
        List<SysUser> allUserList = sysUserService.getAll();
        List<SysUser> unSelectedUserList = Lists.newArrayList();

        Set<Long> selectedUserIdList = selectedUserList.stream()
                .map(SysUser::getId).collect(Collectors.toSet());
        for (SysUser sysUser : allUserList) {
            if (sysUser.getStatus() == 1
                    && !selectedUserIdList.contains(sysUser.getId())) {
                unSelectedUserList.add(sysUser);
            }
        }

        Map<String, List<SysUser>> result = Maps.newHashMap();
        result.put("selected", selectedUserList);
        result.put("unselected", unSelectedUserList);
        return ResponseBean.success(result);
    }

    @PutMapping("/changeUsers.json")
    @ResponseBody
    public ResponseBean changeUser(
            @RequestBody @Valid ChangeUserParam param
    ) {
        sysRoleUserService.changeUser(param.getRoleId(), param.getUserIds());
        return ResponseBean.success();
    }

    @DeleteMapping("/delete.json")
    @ResponseBody
    public ResponseBean deleteDept(@RequestParam("id") Long roleId) {
        log.info("Start deleteDept. roleId: {}", roleId);
        SysRole sysRole = sysRoleService.delete(roleId);
        log.info("End deleteDept. sysRole: {}", sysRole);
        return ResponseBean.success(sysRole);
    }

}
